package com.wyy.test


import io.lettuce.core.RedisURI
import io.lettuce.core.api.reactive.RedisReactiveCommands
import io.lettuce.core.cluster.RedisClusterClient
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection
import org.apache.commons.lang3.StringUtils
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

import java.util.stream.Collectors

/**
 * @Date: 2022/6/28
 * @Author: wyy  
 */
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Disabled
class LettuceClusterTest {

    private RedisClusterClient redisClusterClient

    private StatefulRedisClusterConnection<String, String> connection

    @BeforeAll
    void initClient() {
        println "LettuceTest.initClient"

        String nodes = "localhost:6379,localhost:6380"
        String password = "123123"

        List<RedisURI> redisURIS = nodes.split(",")
                .collect { node ->
                    String[] node_port = StringUtils.split(node, ":");
                    RedisURI.Builder builder = RedisURI.builder();
                    builder.withHost(node_port[0]);
                    builder.withPort(Integer.valueOf(node_port[1]));
                    builder.withPassword(password.toCharArray())
                    return builder.build()
                }

        this.redisClusterClient = RedisClusterClient.create(redisURIS);
        this.connection = this.redisClusterClient.connect()

    }

    @AfterAll
    void close() {
        println "LettuceSingleTest.close"

        this.connection.close()
        this.redisClusterClient.shutdown()
    }

    @Test
    void testList() {
        def reactiveCommands = this.connection.reactive()
        List<String> list = reactiveCommands.lrange("test:list", 0, -1)
                .toStream()
                .collect(Collectors.toList())
        println "list = $list"
    }

}
